package com.zengqingfa.algorithm.cz.chapter02;

import lombok.Data;

/**
 * @author zengqingfa
 * @className Student
 * @description
 * @create 2021/8/6 10:24
 */
@Data
public class Student implements Comparable<Student> {
    private String username;
    private int age;

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }

    public static void main(String[] args) {
        Student stu1 = new Student();
        stu1.setUsername("zhangsan");
        stu1.setAge(26);
        Student stu2 = new Student();
        stu2.setUsername("lisi");
        stu2.setAge(22);
        Comparable max = getMax(stu1, stu2);
        //Student(username=zhangsan, age=26)
        System.out.println(max);
    }

    //测试方法，获取两个元素中的较大值
    public static Comparable getMax(Comparable c1, Comparable c2) {
        int cmp = c1.compareTo(c2);
        if (cmp >= 0) {
            return c1;
        } else {
            return c2;
        }
    }
}